/*
 * ZENworks Notification Icon
 * Copyright (C) Author: Gangadhar Nagesh Metla (Novell, Inc.)
 * nmetla74@gmail.com 
 * Version 1.0
 * 1/3/2009
 * Filename MenuItemHandler.java
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package com.novell.zenworks.agent.zicon;

import java.io.File;
import java.util.Locale;

import com.novell.zenworks.agent.common.Paths;
import com.novell.zenworks.agent.common.ICommonUIEnums.MB_ICON;
import com.novell.zenworks.agent.common.ICommonUIEnums.MB_RESPONSE;
import com.novell.zenworks.agent.common.ICommonUIEnums.MB_TYPE;
import com.novell.zenworks.agent.zbrowser.MessageBoxLauncher;
import com.novell.zenworks.agent.zicon.progress.ZProgressDialog;
import com.novell.zenworks.zicon.common.IZenIconMenuItem;
import com.novell.zenworks.zicon.common.LogUtil;

public class MenuItemHandler implements Runnable
{
    protected IZenIconMenuItem zim = null;
    public MenuItemHandler(IZenIconMenuItem zim)
    {
        this.zim = zim;
    }
    public void handleMenuItem()
    {
        try
        {
            if (zim.getId().equalsIgnoreCase("Help")) // try
                                                      // enums
            {
                org.eclipse.swt.program.Program.launch(Paths.getHelpDirectory(Locale.getDefault()) + File.separator + "overview.html");
            }
            else if (zim.getId().equalsIgnoreCase("Refresh")) // try
                                                         // enums
            { 
                Animator anim = Animator.getInstance();
                anim.setStop(false);
                RefreshUIHandler rh = new RefreshUIHandler(zim);

                Thread t = new Thread(rh);
                t.start();
                ZTrayIcon.getInstance().getDisplay().syncExec(Animator.getInstance().getAnimation());      
            }
            
            else if (zim.getId().equalsIgnoreCase("Show Properties")) // try
                                                                 // enums
            {
                PageApp.launchBrowser(ZTrayIcon.propertyPage.getId());
            }
            else
            {
                Thread t = new Thread(new MenuItemHandler(zim));
                t.start();
            }
        } catch (Exception e1)
        {
            try
            {
                LogUtil.LogException(e1.getMessage(), e1);
                MessageBoxLauncher.ShowMessageBoxHandler(ZTrayIcon.rm.getString("RemotingError"), ZTrayIcon.rm.getString("PropDlgTitle"), MB_TYPE.MBT_OK, MB_ICON.MBI_ERROR, 30, false, MB_RESPONSE.MBR_OK);
                Animator.getInstance().setStop(true);
            } catch (Exception e2)
            {
                Animator.getInstance().setStop(true);
                LogUtil.LogException(e2.getMessage(), e2);
            }
        }
    }

    public void run()
    {
        try
        {
            ZTrayIcon.getServer().invokeMenuItem(zim);
        }
        catch (Exception e1)
        {
            try
            {
                LogUtil.LogException(e1.getMessage(), e1);
                MessageBoxLauncher.ShowMessageBoxHandler(ZTrayIcon.rm.getString("RemotingError"), ZTrayIcon.rm.getString("PropDlgTitle"), MB_TYPE.MBT_OK, MB_ICON.MBI_ERROR, 30, false, MB_RESPONSE.MBR_OK);
                Animator.getInstance().setStop(true);
            } catch (Exception e2)
            {
                Animator.getInstance().setStop(true);
                LogUtil.LogException(e2.getMessage(), e2);
            }
        }        
    }
}
